﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using rbac.Contracts.DTO.Input;
using rbac.Contracts.DTO.Output;
using rbac.Contracts.Interface;

namespace rbac.API.Controllers
{
    /// <summary>
    /// 用户管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
/*    [HiddenApi]*/
    
    public class UserController : ControllerBase
    {
        /// <summary>
        /// 引用服务
        /// </summary>
        private readonly IUserService service;
        private readonly TokenService _tokenService;
        private readonly ILogger<UserController> _logger;
        public UserController(IUserService service, TokenService tokenService,ILogger<UserController>logger)
        {
            this.service = service;
             this._tokenService = tokenService;
            this._logger = logger;
        }
        /// <summary>
        /// 用户查询
        /// </summary>
        /// <remarks>用户查询的详细说明</remarks>
        /// <returns></returns>
        [HttpPost]//请求谓词
        [Authorize]
        public Results<PageListOutput<UserOutput>> GetUsers([FromBody]GetUsersInput input)
        {
            return Results<PageListOutput<UserOutput>>.DataResult(service.GetUsers(input));
        }
        /* /// <summary>
         /// 用户查询2
         /// </summary>
         /// <remarks>用户查询的详细说明</remarks>
         /// <returns></returns>
         [HttpGet]
         [HiddenApi]  //在swagger隐藏显示
         *//*[NonAction]*//* //阻断前端调用
         public List<UserOutput> GetUsers2()
         {
             return service.GetUsers();
         }*/
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="input">用户名和密码</param>
        /// <returns></returns>
        [HttpPost]
        public Results<UserOutput> login([FromBody]LoginInput input)
        {
            var list = service.Login(input);
            if (list!=null)
            {
                /*_logger.LogInformation($"{list.NickName}登录成功");*/
                var token = _tokenService.GenerateToken(list);
               return Results<UserOutput>.TokenResult(token,list);
            }
            _logger.LogError($"账号：{input.UserName},密码：{input.Pwd}不正确");
            return Results<UserOutput>.FailResult("用户或者密码不正确");
           
           
        }
    }
}
